/**
 * 
 */
package id.web.faisalabdillah.xtimesheet.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.stereotype.Component;

import id.web.faisalabdillah.xtimesheet.dao.IUserDao;
import id.web.faisalabdillah.xtimesheet.domain.User;

/**
 * @author faitz
 *
 */
@Component
public class UserDaoImpl implements IUserDao {

	private JpaTemplate jpaTemplate;
	
	@Autowired
	protected void setJpaTemplate(EntityManagerFactory entityManagerFactory){
		jpaTemplate=new JpaTemplate(entityManagerFactory);
	};
	
	
	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#insert(id.web.faisalabdillah.xtimesheet.domain.User)
	 */
	@Override
	public void insert(User user) {
		jpaTemplate.persist(user);
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#update(id.web.faisalabdillah.xtimesheet.domain.User)
	 */
	@Override
	public void update(User user) {
		jpaTemplate.merge(user);

	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#remove(id.web.faisalabdillah.xtimesheet.domain.User)
	 */
	@Override
	public void remove(User user) {
		jpaTemplate.remove(user);
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#listAll()
	 */
	@Override
	public List<User> listAll() {
		return jpaTemplate.find("select u from User u");
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#listAllPaged(int, int)
	 */
	@Override
	public List<User> listAllPaged(int first, int max) {
		final int f=first;
		final int m=max;
		return jpaTemplate.execute(new JpaCallback<List<User>>() {

			@Override
			public List<User> doInJpa(EntityManager em)
					throws PersistenceException {
				Query q=em.createQuery("select u from User u");
				q.setFirstResult(f);
				q.setMaxResults(m);
				return q.getResultList();
			}
		});
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#searchByName(java.lang.String)
	 */
	@Override
	public List<User> searchByName(String name) {
		return jpaTemplate.find("select u from User u where UPPER(u.nama) = UPPER(?1)", new Object[]{"%"+name+"%"});
	}


	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#searchByNamePaged(java.lang.String, int, int)
	 */
	@Override
	public List<User> searchByNamePaged(String name, int first, int max) {
		final String n=name;
		final int f=first;
		final int m=max;
		return jpaTemplate.execute(new JpaCallback<List<User>>() {

			@Override
			public List<User> doInJpa(EntityManager arg0)
					throws PersistenceException {
				Query q=arg0.createQuery("select u from User u where UPPER(u.nama) = UPPER(?1)");
				q.setParameter(1, "%"+n+"%");
				q.setFirstResult(f);
				q.setMaxResults(m);
				return q.getResultList();
			}
		});
	}


	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#findById(java.lang.String)
	 */
	@Override
	public User findById(String id) {
		return jpaTemplate.find(User.class, id);
	}


	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#sizeAll()
	 */
	@Override
	public int sizeAll() {
		return jpaTemplate.execute(new JpaCallback<Integer>() {

			@Override
			public Integer doInJpa(EntityManager arg0)
					throws PersistenceException {
				Query q=arg0.createQuery("select count(1) from User");
				return (Integer) q.getSingleResult();
			}
		});
	}


	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IUserDao#searchByNameSize(java.lang.String)
	 */
	@Override
	public int searchByNameSize(String name) {
		return jpaTemplate.execute(new JpaCallback<Integer>() {

			@Override
			public Integer doInJpa(EntityManager arg0)
					throws PersistenceException {
				Query q=arg0.createQuery("select count(1) from User u where UPPER(u.nama) = UPPER(?1)");
				return (Integer) q.getSingleResult();
			}
		});
	}

}
